﻿#include "Supply.h"
#include"Sprite.h"
#include"WeaponBank.h"
Supply::Supply(int x, int y, Type type, const QVector2D& vector, const int Steplength)
	:Articles(x,y),
	m_Typr(type)
{
	setProperty(vector, Steplength);
	init();
}

//void Supply::bindPlane(Sprite* const plane)
//{
//	m_Plane = plane;
//}

void Supply::setProperty(const QVector2D& vector, const int Steplength)
{
	m_vector = vector;
	m_Steplength = Steplength;
}

bool Supply::Collider(Sprite* p)
{
	if (Articles::Collider(*p))
	{
		startSupply(p);
		destroy();
		return true;
	}
	return false;
}

void Supply::init()
{
	m_temp = getCurrentTime();
}

void Supply::update()
{
	// 超出屏幕自动释放
	if (!getModelRect().intersects(m_Window))
	{
		destroy();
	}
	else
	{
		setCollider(getModelRect());
	}
}

//void Supply::draw(QPainter* painter)
//{
//
//}

void Supply::AutoMove()
{
	if (!isActive() || getHp() <= 0)
		return;
	if (m_pause)
	{
		m_temp = getCurrentTime();
		return;
	}
	qreal len = m_Steplength * (getCurrentTime() - m_temp) / 1000.0;
	if (len < 1.0)
	{
		m_SumLen += len;
	}
	if (m_SumLen > 1.0)
	{
		len = m_SumLen;
		m_SumLen = 0;
	}

	m_temp = getCurrentTime();
	QPoint pos(m_vector.x() * len, m_vector.y() * len);
	move(getPos() + pos);
}

void Supply::startSupply(Sprite* plane)
{
	if (plane == nullptr)
		return;
	/*qDebug() << "补给类型判断"<< m_Typr;*/
	if (m_Typr == Type::bullet)
		bloodSupply(plane);
	else if (m_Typr == Type::bomb)
		bombSupply(plane);
}

void Supply::bloodSupply(Sprite* plane)
{
	plane->getWeaponBank()->addBulletLevel(1);
}

void Supply::bombSupply(Sprite* plane)
{
	plane->addBombNum(1);
}
